package com.shenglue.ssi.dao;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.shenglue.ssi.domain.BuyBackMain;

public class BuyBackMainDaoImpl extends SqlMapClientDaoSupport implements BuyBackMainDao {

	/**
	 * 查询所有入库退货单主表信息
	 */
	public List<BuyBackMain> selectAllBuyBackMain() {
		@SuppressWarnings("unchecked")
		List<BuyBackMain> list = this.getSqlMapClientTemplate().queryForList(
		"selectAllBuyBackMain");

		if (list != null && list.size() > 0) {
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 按条件查询 入库退货单主表信息
	 */
	public List<BuyBackMain> selectBuyBackMainByCond( BuyBackMain buyBackMain ){
		@SuppressWarnings("unchecked")
		List<BuyBackMain> list = this.getSqlMapClientTemplate().queryForList(
		"selectBuyBackMainByCond", buyBackMain);

		if (list != null && list.size() > 0) {
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 增加入库退货单主表记录
	 */
	public boolean saveBuyBackMain(BuyBackMain buyBackMain) {

		try {
			Object oRtn = this.getSqlMapClientTemplate().insert( "insertBuyBackMain", buyBackMain );
			// 已改正 来自网上的写法。已有文章讨论

			//插入成功 返回true
			return true;
		}
		catch ( DataAccessException e ) {
			//失败 返回false
			return false;
		}
	}

	/**
	 * 删除入库退货单主表记录
	 */
	public boolean deleteBuyBackMain(int buyBackId) {
		int flag = this.getSqlMapClientTemplate().delete( "deleteBuyBackMainById", buyBackId );
		if( flag > 0 ) {
			return true;
		}
		return false;
	}

	/**
	 * 修改入库退货单主表记录
	 */
	public boolean updateBuyBackMain(BuyBackMain buyBackMain) {

		int flag = this.getSqlMapClientTemplate().update("updateBuyBackMain", buyBackMain);
		if( flag > 0 ) {
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 为修改，根据 Id 查一条 入库退货单主表记录
	 */
	public BuyBackMain getOneBuyBackMainForUpdate( int buyBackId ) {
		Object oBuyBackMain = this.getSqlMapClientTemplate().
		queryForObject( "getOneBuyBackMainForUpdate", buyBackId );

		BuyBackMain buyBackMain = (BuyBackMain)oBuyBackMain;

		return buyBackMain;

	}
}
